package com.yxlgateway.gateway.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yxlcommon.common.pojo.Author;
import com.yxlcommon.common.pojo.Demands;
import com.yxlcommon.common.pojo.RespBean;
import com.yxlgateway.service.AuthorService;
import com.yxlgateway.service.DemandsService;
import com.yxlcommon.common.vo.DemandsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: yxl
 * @since: 2021-11-30
 **/
@Slf4j
@RestController
@RequestMapping("/demands")
public class DemandsController {

    @Autowired
    private DemandsService demandsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AuthorService authorService;

    @PostMapping("/selectAllDemands")
    public RespBean selectAllDemands(){
        return RespBean.operationSuccess("查询成功",200,demandsService.listDemands());
    }

    @PostMapping("/addDemands")
    public RespBean addDemands(@RequestBody Demands demands){
        if (demands.getClosingDate().getTime()-new Date().getTime() < 0){
            return RespBean.failMessage("截止时间不能早于当前时间！",null);
        }
        demandsService.addDemands(demands);
        redisTemplate.opsForValue().set( "demandId"+demands.getDemandId(),demands.getDemandId(), demands.getClosingDate().getTime()-demands.getCreatedTime().getTime(), TimeUnit.MILLISECONDS);
        return RespBean.operationSuccess("发布成功",200,"ok");
    }

    @PostMapping("/selectDemandsByUsername")
    public RespBean selectDemandsByUsername(@RequestBody Demands demands){
        return RespBean.operationSuccess("查询成功",200,demandsService.selectDemandsByUsername(demands));
    }

    @PostMapping("/selectDemandsByUserId")
    public RespBean selectDemandsByUserId(@RequestBody DemandsVo demands){
        return RespBean.operationSuccess("查询成功",200,demandsService.selectDemandsByUserId(demands));
    }

    @PostMapping("/selectDemandById")
    public RespBean selectDemandById(@RequestBody Demands demands){
        return demandsService.selectDemandDetailById(demands.getDemandId());
    }

    @PostMapping("/searchDemandsByKeyword")
    public RespBean searchDemandsByKeyword(@RequestParam String searchText){
        return RespBean.operationSuccess("搜索成功",200,demandsService.getDemandByKeyword(searchText));
    }

    @PostMapping("/searchDemandByCategory")
    public RespBean<List<DemandsVo>> searchDemandByCategory(@RequestBody Demands demands){
        return demandsService.selectDemandByCategory(demands);
    }

    @PostMapping("/searchDemandsPage")
    public RespBean<IPage> searchDemandsPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                         @RequestParam(defaultValue = "10") Integer size,
                                         @RequestBody Demands demands){
        return demandsService.searchDemandsPage(currentPage,size,demands);
    }

    @PostMapping("/changeDemandById")
    public RespBean<Integer> changeDemandById(@RequestBody Demands demands){
        return demandsService.updateDemandById(demands);
    }

    @PostMapping("/deleteDemandById")
    public RespBean<Integer> deleteDemandById(@RequestBody Demands demands){
        return demandsService.deleteDemandById(demands);
    }

    @PostMapping("/recruitDemand")
    public RespBean recruitDemand(@RequestBody Author author){
        return authorService.selectAuthorById(author);
    }

}
